using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Diagnostics;
using Microsoft.SharePoint;

namespace CKS.EBE.WebControls
{
    public class List : XmlBasedControl
    {
        private string _ListName;
        private bool _FoldersEnabled;
        private string _FolderQueryStringKey = "folder";
        private bool _GroupFolders;

        public bool GroupFolders
        {
            get { return _GroupFolders; }
            set { _GroupFolders = value; }
        }

        public string FolderQueryStringKey
        {
            get { return _FolderQueryStringKey; }
            set { _FolderQueryStringKey = value; }
        }

        public bool FoldersEnabled
        {
            get { return _FoldersEnabled; }
            set { _FoldersEnabled = value; }
        }

        public string ListName
        {
            get { return _ListName; }
            set { _ListName = value; }
        }

        protected string CurrentPath
        {
            get
            {
                return Page.Request[FolderQueryStringKey];
            }
        }

        protected override XmlDocument BuildDocument()
        {
            try
            {
                if (CurrentPath != null)
                    Transform.XslArguments.AddParam("CurrentPath", "", CurrentPath);

                SPList list = Helper.GetListByFolderName(Web, ListName);
                string listItemXml = list.Items.Xml;
                return !GroupFolders ? CleanXml(listItemXml) : CreateFolderXML(list);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);

                var oError = new XmlDocument();
                oError.LoadXml(string.Format("<Error>{0}</Error>", ex.Message));
                return oError;
            }
        }

        private XmlDocument CreateFolderXML(SPList oList)
        {
            try
            {
                StringBuilder xml = new StringBuilder();
                StringWriter sw = new StringWriter(xml);
                XmlTextWriter oWriter = new XmlTextWriter(sw);

                oWriter.WriteStartElement("Root");

                SPFolder rootFolder = oList.RootFolder;

                //if (CurrentPath != null)
                //    oFolder = Web.Site.RootWeb.GetFolder(Web.Site.RootWeb.Url + CurrentPath); //rootSite
                //else
                //    oFolder = oList.RootFolder;

                if (rootFolder != null)
                    AddFolder(oWriter, rootFolder);

                XmlDocument oDoc = new XmlDocument();

                oWriter.Close();
                oDoc.LoadXml(xml.ToString());

                return oDoc;
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
                throw new Exception("Failed to create folder XML: " + ex.Message, ex);
            }
        }

        private void AddFolder(XmlWriter oWriter, SPFolder oFolder)
        {
            oWriter.WriteStartElement("folder");
            oWriter.WriteAttributeString("url", oFolder.Url);
            oWriter.WriteAttributeString("serverRelativeUrl", oFolder.ServerRelativeUrl);
            oWriter.WriteAttributeString("name", oFolder.Name);

            if (oFolder.Item != null)
            {
                string sXML = CleanXml(oFolder.Item.Xml).OuterXml;
                oWriter.WriteRaw(sXML.Substring(6, sXML.Length-13));
            }

            foreach (SPFolder oSubFolder in oFolder.SubFolders)
            {
                if (oSubFolder.Item != null)
                    AddFolder(oWriter, oSubFolder);
            }

            oWriter.WriteStartElement("rows");
            foreach (SPFile oFile in oFolder.Files)
            {
                if (oFile.Item != null)
                {
                    string sXML = CleanXml(oFile.Item.Xml).OuterXml;
                    oWriter.WriteRaw(sXML.Substring(6, sXML.Length-13));
                }
            }

            try
            {
                SPList oList = Helper.GetListByFolderName(Web, ListName);
                SPQuery oQuery = new SPQuery {Folder = oFolder};
                SPListItemCollection oCollection = oList.GetItems(oQuery);
                foreach (SPListItem oItem in oCollection)
                {
                    string sXML = CleanXml(oItem.Xml).OuterXml;
                    oWriter.WriteRaw(sXML.Substring(6, sXML.Length - 13));
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }

            oWriter.WriteEndElement();
        }
    }
}
